﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

using DbSharp.Framework.Storage;
using DbSharp.Framework.Storage.IO;

namespace DbSharp.Framework.Diff
{
    public class DiffExcelOutputer : ISetDiffOutputer
    {
        private void Prepare(string xlsFile, out bool isExcel2007)
        {
            if (File.Exists(xlsFile))
                File.Delete(xlsFile);

            string fileExt = Path.GetExtension(xlsFile);
            isExcel2007 = string.Equals(fileExt, ".xlsx", StringComparison.OrdinalIgnoreCase);
        }

        private bool IsEmptyOrNull(IEnumerable<XElement> records)
        {
            if (records == null)
                return true;
            else if (records.Count() == 0)
                return true;
            else
                return false;
        }

        #region ISetDiffOutputer Members

        public void WriteChangedLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> addedLines, IEnumerable<XElement> deletedLines)
        {
            bool isExcel2007;
            Prepare(outputFileName, out isExcel2007);
            title = string.Empty;
            string excelSheetTitle = ExcelFileWriterHelper.GetSheetDefaultTitle();

            List<string> listRowsGroupName = new List<string>();
            List<IEnumerable<XElement>> listRowsGroup = new List<IEnumerable<XElement>>();
            List<long> listExportRecordCount = new List<long>();

            listRowsGroupName.Add("surplus");
            listRowsGroup.Add(addedLines);
            listExportRecordCount.Add(long.MaxValue);

            listRowsGroupName.Add("absent");
            listRowsGroup.Add(deletedLines);
            listExportRecordCount.Add(long.MaxValue);

            var fileWriter = new ExcelFileWriter(outputFileName, isExcel2007);
            string groupColumnHeader = "DiffCategory";

            fileWriter.SaveToFile(excelSheetTitle, groupColumnHeader, fieldsOutputNameList, listRowsGroupName, listRowsGroup, listExportRecordCount);
        }

        public void WriteCommonLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> commonLines)
        {
            bool isExcel2007;
            Prepare(outputFileName, out isExcel2007);
            title = string.Empty;
            string excelSheetTitle = ExcelFileWriterHelper.GetSheetDefaultTitle();

            List<string> listRowsGroupName = new List<string>();
            List<IEnumerable<XElement>> listRowsGroup = new List<IEnumerable<XElement>>();
            List<long> listExportRecordCount = new List<long>();

            listRowsGroupName.Add("common");
            listRowsGroup.Add(commonLines);
            listExportRecordCount.Add(long.MaxValue);

            var fileWriter = new ExcelFileWriter(outputFileName, isExcel2007);
            string groupColumnHeader = "DiffCategory";

            fileWriter.SaveToFile(excelSheetTitle, groupColumnHeader, fieldsOutputNameList, listRowsGroupName, listRowsGroup, listExportRecordCount);
        }

        public void WriteUnionLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> commonLines, IEnumerable<XElement> addedLines, IEnumerable<XElement> deletedLines)
        {
            bool isExcel2007;
            Prepare(outputFileName, out isExcel2007);
            title = string.Empty;
            string excelSheetTitle = ExcelFileWriterHelper.GetSheetDefaultTitle();

            List<string> listRowsGroupName = new List<string>();
            List<IEnumerable<XElement>> listRowsGroup = new List<IEnumerable<XElement>>();
            List<long> listExportRecordCount = new List<long>();

            listRowsGroupName.Add("common");
            listRowsGroup.Add(commonLines);
            listExportRecordCount.Add(long.MaxValue);

            listRowsGroupName.Add("surplus");
            listRowsGroup.Add(addedLines);
            listExportRecordCount.Add(long.MaxValue);

            listRowsGroupName.Add("absent");
            listRowsGroup.Add(deletedLines);
            listExportRecordCount.Add(long.MaxValue);

            var fileWriter = new ExcelFileWriter(outputFileName, isExcel2007);
            string groupColumnHeader = "DiffCategory";

            fileWriter.SaveToFile(excelSheetTitle, groupColumnHeader, fieldsOutputNameList, listRowsGroupName, listRowsGroup, listExportRecordCount);
        }

        #endregion
    }


}
